/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package actions;

import com.opensymphony.xwork2.validator.annotations.EmailValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.ValidatorType;
import com.paris5.master2.guitarmaster.bean.EcomUser;
import com.paris5.master2.guitarmaster.exception.EcomException;
import com.paris5.master2.guitarmaster.exception.EcomQueryException;
import com.paris5.master2.guitarmaster.exception.EcomValidationException;
import com.paris5.master2.guitarmaster.service.ServiceConvertorRemote;
import com.paris5.master2.guitarmaster.service.ServiceCustomerRemote;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Leticia
 */
public class Connection extends EcomAction {

    private String login;
    private String password;
    private EcomUser ecomUser;
    private double rate;

    @Override
    public String execute() throws EcomException {
        ServiceCustomerRemote serviceCustomerRemote = lookupServiceCustomerRemote();
        try {
            ecomUser = serviceCustomerRemote.authentificate(login, password);
            if (ecomUser == null) {
                setMessage("Utilisateur introuvable");
                return INPUT;
            } else {    
                if (ecomUser.getCountry().getCode().equals("EN")) {
                    ServiceConvertorRemote serviceConvertorRemote = lookupServiceCovertorRemote();
                    ecomUser.setBalance(new Float(serviceConvertorRemote.convert(ecomUser.getBalance(), "EUR", "USD")));
                } 
                saveSession("USER", ecomUser);
                return SUCCESS;
            }
        } catch (EcomValidationException ex) {
            Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
        } catch (EcomQueryException ex) {
            Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
        }
        return ERROR;
    }

    @RequiredStringValidator(message = "Login requis.")
    @EmailValidator(type = ValidatorType.SIMPLE, fieldName = "email",
    message = "Veuillez saisir un email valide.")
    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    @RequiredStringValidator(message = "Mot de passe requis.")
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public EcomUser getEcomUser() {
        return ecomUser;
    }

    public void setEcomUser(EcomUser ecomUser) {
        this.ecomUser = ecomUser;
    }

    public double getRate() {
        return rate;
    }

    public void setRate(double rate) {
        this.rate = rate;
    }        
}
